/**
 * This file is part of XaMauLo 
 * File :                       DocumentXML.scala
 * Author's email :             laure.conte@gmail.com
 *                              sophie.couillez@gmail.com
 **/

package utils;
import main.Validator;
import scala.xml._;
import org.xml.sax._;
import scala.xml.include.sax._;
import org.xml.sax.helpers.XMLReaderFactory;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.File;


/**
  * This class permit to parse an XML files, or read it in order to analyze the syntax.
 */

class DocumentXML {
  var fichierResult:String="";
  
  def readXML(ficXML : String, ficXSD : String){
    println(ficXML+" "+ficXSD);
   val xsd : SchemaXML = new SchemaXML(ficXSD);
   val valid : Validator =new Validator(xsd,fichierResult);
  
    try{
      var parser: XMLReader = XMLReaderFactory.createXMLReader(); 
      val includer:XIncludeFilter=new XIncludeFilter(); 
      includer.setParent(parser);
      includer.setContentHandler(new ParcoursXML(valid));
      includer.parse(ficXML);
      
      
      println(valid.commentaireFic.toString());
      if(!fichierResult.equals("")){
        writeFile(fichierResult,valid,ficXML,ficXSD);
      }
    }
    //si le fichier xml n'est pas correct alors on retourne une exception!!
         catch{
           case e: org.xml.sax.SAXParseException => println("Error line "+e.getLineNumber+" "+e.getMessage());
           case e : Exception =>e.printStackTrace();println("Probleme "+e.getMessage());
        }
   
  }
  
  def writeFile(fic:String,valid: Validator, ficXML :String, ficXSD : String){
    var commentaireFic : StringBuilder=new StringBuilder();
    try
    { 
      commentaireFic.append("Resume des tests\n");
      commentaireFic.append("---------------------------\n\n");
      commentaireFic.append("XML File : ");
      commentaireFic.append(ficXML);
      commentaireFic.append("          XSD File : ");
      commentaireFic.append(ficXSD);
      commentaireFic.append("\n\n\nStructure \n");
      commentaireFic.append("---------------------------\n\n");
      commentaireFic.append(valid.commentaireFic);
      commentaireFic.append("\n\nErrors \n");
      commentaireFic.append("---------------------------\n\n");
      if(valid.listError.size==0){commentaireFic.append("Test complete without errors!")}
      for(val tmp<-valid.listError.reverse){
       commentaireFic.append(tmp.typeError+" ~~> "+tmp.descrErr+" at line : "+tmp.ligne+" column : "+tmp.column+"\n\n");
      }
    	var f=new File(fic);      
        val out = new FileOutputStream(fic);
        val p = new PrintStream(out);
        p.print(commentaireFic.toString);
        p.close();
    }
catch{
    case e : Exception =>e.printStackTrace();println("Probleme "+e.getMessage());
    }
    
  }
  
  
  def verifyXML(fic : String):Boolean =  {
    try{
      var parser: XMLReader = XMLReaderFactory.createXMLReader(); 
      val includer:XIncludeFilter=new XIncludeFilter(); 
      includer.setParent(parser);
      includer.parse(fic);
      println("The file is correct!");
      return true;
    }
    //si le fichier xml n'est pas correct alors on retourne une exception!!
         catch{
           case e: org.xml.sax.SAXParseException => println("Error line "+e.getLineNumber+" "+e.getMessage());
           case e : Exception =>println("Problem "+e.getMessage());
        }
   return false;
  }
  def showManPage(){
    
    println("XaMauLo");
    println("\nName");
    println("------\n");
    println("  XaMauLo - command line xml xsd validator");
    println("\nSynopsis");
    println("--------\n");
    println("  xamaulo [-verify | -validate | -out]");
    println("\nIntroduction")
    println("-------------\n");
    println("  This project can validate an XML document with its XML schema (XSD). ");
    println("  It works on both Windows and Linux with only the Java Virtual Machine."); 
    println("  It is developed in Scala with Java by two french students");
    println("\nOptions");
    println("--------\n");
    println("  -verify");
    println("    Verify an XML or XSD document. It will return the problem of the XML file");
    println("  -validate");
    println("    Test if the XML document is valid with the XML schema");
    println("  -out");
    println("    Save the results on a given File");
    println("  -version");
    println("     Show the current version of the application");
    println("   Examples");
    println("    Do \"Xamaulo -validate test1.xml test2.xsd\" to validate an XML");
    println("    Do \"Xamaulo -validate test1.xml test2.xsd -out res.txt\" to validate an XML and save the results on the file");
    println("\nAuthors");
    println("--------\n");
    println("  Sophie Couillez - Laure Conte");
    
  }
}
